#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/12/13 23:23
# @Author  : 唐松
# @File    : debugtalk.py
# @Software: PyCharm

import random
from configs.config import read_extract_yaml, read_config_file
import re


class DebugTalk:

    def get_extract_data(self, node_name, out_format=None):
        """
        获取extract.yaml文件数据，首先判断out_format是否为数字类型。如果不是就获取下一个节点的value
        :param node_name: extract.yaml文件中的key
        :param out_format: str类型，0：随机去读取；
                                  -1：读取列表全部数据，返回字符串格式；
                                  -2：读取全部，返回是列表格式
                                  其他值：按照顺序读取
        :return:
        """
        data = read_extract_yaml(node_name)
        # 有第二个参数，且为数字的情况下
        if out_format is not None and bool(re.compile(r'^[+-]?\d+$').match(str(out_format))):
            out_format = int(out_format)
            data_value = {
                out_format: self.seq_read(data, out_format),
                0: random.choice(data),
                -1: ','.join(data),
                -2: ','.join(data).split(',')
            }
            data = data_value[out_format]
        else:
            data = read_extract_yaml(node_name, out_format)
        return data

    @classmethod
    def seq_read(cls, data, out_format):
        """获取extract.yaml，第二个参数，不为0, -1, -2的情况下"""
        if out_format not in [0, -1, -2]:
            return data[out_format - 1]
        else:
            return None

    @classmethod
    def params_md5(cls):
        pass

    @classmethod
    def get_now_time(cls):
        return '2024-01-01'

    # 用户获取config.yaml文件-url
    @classmethod
    def get_base_url(cls, node_name):
        """
        :param node_name: base_url
        :return:
        """
        return read_config_file('base', node_name)

    @classmethod
    def get_mysql_config(cls, node_name):
        """
        :param node_name: mysql
        :param node_name:
        :return:
        """
        return read_config_file('mysql', node_name)

    @classmethod
    def get_headers(cls, params_type):
        headers_mapping = {
            'data': {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
            'json': {'Content-Type': 'application/json;charset=UTF-8 '}
        }
        header = headers_mapping.get(params_type)
        if header is None:
            raise ValueError('不支持其他类型的请求头设置')
        return header

    @classmethod
    def get_random_number(cls, min, max):
        return random.randint(int(min), int(max))


if __name__ == '__main__':
    debug = DebugTalk()
    res = debug.get_extract_data('access_token')
    print(res)
